// pages/optional/components/match/index.js
const {
  matchSchool,
  matchSpecial
} = require('../../../../api/api')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    mode: {
      type: String,
      value: 'school'
    },
    visible: {
      type: Boolean,
      value: false
    },
    checkedItem: {
      type: Object,
      value: {}
    },
    props: {
      type: Object,
      value: {
        schoolName: 'schoolName',
        schoolId: 'schoolId',
        specialName: 'specialName',
        majorNum: 'num',
        schoolNum: 'num',
        spname:'specialName'
      }
    }
  },
  observers: {
    'checkedItem': function (checkedItem) {
      if (this.data.visible) {
        this.setData({
          pageNo: 1,
          list: []
        })
        wx.showLoading()
        this.loadData(checkedItem)
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    bottomLift: getApp().globalData.bottomLift,
    pageNo: 1,
    pageSize: 10
  },

  /**
   * 组件的方法列表
   */
  methods: {
    close() {
      this.setData({
        visible: false
      })
    },

    async loadData() {
      const {
        checkedItem,
        props,
        pageSize,
        pageNo,
        mode
      } = this.data
      const optionalData = wx.getStorageSync('optionalChooseFilters')
      const params = {
        type: checkedItem.type,
        pageNo,
        pageSize,
        provinceId: optionalData.province,
        year: optionalData.year,
        subjects: checkedItem.subjects
      }
      if (mode == 'school') {
        params.schoolId = checkedItem[props.schoolId]
      } else {
        params.specialName = checkedItem[props.spname]
      }

      wx.hideLoading()
      if (mode == 'major') {
        // console.log(params)
        const {
          result
        } = await matchSchool(params)
        result.records.map((item) => {
          let array = []
          if (item.isProject985 === 1) {
            array.push('985')
          }
          if (item.isProject211 === 1) {
            array.push('211')
          }
          if (item.isDoubleTop === 1) {
            array.push('双一流')
          }
          if (item.isStrongPlan === 1) {
            array.push('强基计划')
          }
          if (item.isDoubleHighPlan === 1) {
            array.push('双高计划')
          }
          if (item.isEducationDept === 1) {
            array.push('教育部直属')
          }
          array.push(item.provincesName)
          array.push(item.schoolType)
          item['filter'] = array.join('/')
        })
        this.setData({
          ['list[' + (pageNo - 1) + ']']: result.records,
          pageNo: pageNo + 1,
        })
      } else {
        const {
          result
        } = await matchSpecial(params)
        result.records.map((item) => {
          if (item.spname.indexOf('（') > -1) {
            const index = item.spname.indexOf('（')
            item.spname = item.spname.substring(0, index)
          }
        })
        this.setData({
          ['list[' + (pageNo - 1) + ']']: result.records,
          pageNo: pageNo + 1,
        })
      }

    },
    loadMore() {
      const {
        list,
        pageSize,
      } = this.data
      const count = list.length - 1
      if (list[count].length < pageSize) {
        return
      }
      this.loadData()
    },
  }
})